package com.example.anydo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.anydo.R;
import com.example.anydo.bean.CommonBean;
import com.example.anydo.constant.Url;
import com.example.anydo.net.DataBack;
import com.example.anydo.net.NetClient;
import com.example.anydo.util.pub_util.SPUtil;
import com.example.anydo.util.pub_util.Util;

public class ModifyPasswordActivity extends AppCompatActivity {


    private ImageView back;
    private EditText oldPsw;
    private EditText psw;
    private EditText pswCheck;
    private Button comit;
    private String userId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);

        back=(ImageView)findViewById(R.id.back);
        oldPsw=(EditText)findViewById(R.id.oldpsw);
        psw=(EditText)findViewById(R.id.psw);
        pswCheck=(EditText)findViewById(R.id.pswCheck);
        comit=(Button)findViewById(R.id.comit);

        userId = SPUtil.getUserId(this);

        back.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                finish();
            }
        });

        comit.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String oldPassword=oldPsw.getText().toString();
                String password=psw.getText().toString();
                String pswCommit=pswCheck.getText().toString();
                if(checkInfo(oldPassword, password, pswCommit)){
                    startRegister(userId,oldPassword,pswCommit);
                }
            }
        });
    }


    private void startRegister(String id, String oldPsw, String psw) {
        NetClient.getInstance()
                .withUrl(Url.BASE_URL + Url.CHANGE_PSW_URL)
                .activity(this)
                .setPlayer("请稍后")
                .addParams("id",id)
                .addParams("oldPassword",oldPsw)
                .addParams("newPassword",psw)
                .asObject(CommonBean.class)
                .doGet(new DataBack<CommonBean>() {
                    @Override
                    public void onResponse(CommonBean data) {

                        if(data.getCode() == 200){
                            Util.toast(ModifyPasswordActivity.this,data.getMessage());
                            finish();
                        }else {
                            Util.toast(ModifyPasswordActivity.this,data.getMessage());
                        }
                    }
                    @Override
                    public void onFailure(String msg) {
                        Util.toast(ModifyPasswordActivity.this,"出了一点点问题...");
                    }
                });
    }

    private boolean checkInfo(String oldPassword,String newPsw, String newPswCheck){

        if(oldPassword.equals("")){
            Util.toast(this,"请输入旧密码");
            return false;
        }else if(newPswCheck.equals("")){
            Util.toast(this,"请输入新密码");
            return false;
        }else if(newPsw.equals("")) {
            Util.toast(this, "请验证密码");
            return false;
        } else if(newPsw.length()<6){
            Util.toast(this,"密码至少为6位字符");
            return false;
        } else if(!newPswCheck.equals(newPsw)){
            Util.toast(this,"两次输入密码不一致");
            return false;
        }
        return true;
    }
}
